home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / contrib / gle.2.1 / demo / joinstyle / joinoffset.c next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  6.2 KB  |  262 lines

  1.  
  2. /* cylinder drawing demo */
  3. /* this demo demonstrates the various join styles
  4.  
  5. /* required include files */
  6. #include <GL/gl.h>
  7. #include "glut.h"
  8. #include "GL/tube.h"
  9.  
  10. /* ------------------------------------------------------- */
  11.  
  12. /* the arrays in which we will store the polyline */
  13. #define NPTS 100
  14. double points [NPTS][3];
  15. float colors [NPTS][3];
  16. int idx = 0;
  17.  
  18. /* some utilities for filling that array */
  19. #define PNT(x,y,z) {             \
  20.    points[idx][0] = x;             \
  21.    points[idx][1] = y;             \
  22.    points[idx][2] = z;            \
  23.    idx ++;                \
  24. }
  25.  
  26. #define COL(r,g,b) {             \
  27.    colors[idx][0] = r;             \
  28.    colors[idx][1] = g;             \
  29.    colors[idx][2] = b;            \
  30. }
  31.  
  32. /* the arrays in which we will store the contour */
  33. #define NCONTOUR 100
  34. double contour_points [NCONTOUR][2];
  35. int cidx = 0;
  36.  
  37. /* some utilities for filling that array */
  38. #define C_PNT(x,y) {             \
  39.    contour_points[cidx][0] = x;        \
  40.    contour_points[cidx][1] = y;     \
  41.    cidx ++;                \
  42. }
  43.  
  44.  
  45. /* ------------------------------------------------------- */
  46. /* 
  47.  * Initialize a bent shape with three segments. 
  48.  * The data format is a polyline.
  49.  *
  50.  * NOTE that neither the first, nor the last segment are drawn.
  51.  * The first & last segment serve only to determine that angle 
  52.  * at which the endcaps are drawn.
  53.  */
  54.  
  55. void InitCyl (void) {
  56.  
  57.    COL (0.0, 0.0, 0.0);
  58.    PNT (16.0, 0.0, 0.0);
  59.  
  60.    COL (0.2, 0.8, 0.5);
  61.    PNT (0.0, -16.0, 0.0);
  62.  
  63.    COL (0.0, 0.8, 0.3);
  64.    PNT (-16.0, 0.0, 0.0);
  65.  
  66.    COL (0.8, 0.3, 0.0);
  67.    PNT (0.0, 16.0, 0.0);
  68.  
  69.    COL (0.2, 0.3, 0.9);
  70.    PNT (16.0, 0.0, 0.0);
  71.  
  72.    COL (0.2, 0.8, 0.5);
  73.    PNT (0.0, -16.0, 0.0);
  74.  
  75.    COL (0.0, 0.0, 0.0);
  76.    PNT (-16.0, 0.0, 0.0);
  77.  
  78.    C_PNT (-0.8, -0.5);
  79.    C_PNT (-1.8, 0.0);
  80.    C_PNT (-1.2, 0.3);
  81.    C_PNT (-0.7, 0.8);
  82.    C_PNT (-0.2, 1.3);
  83.    C_PNT (0.0, 1.6);
  84.    C_PNT (0.2, 1.3);
  85.    C_PNT (0.7, 0.8);
  86.    C_PNT (1.2, 0.3);
  87.    C_PNT (1.8, 0.0);
  88.    C_PNT (0.8, -0.5);
  89.  
  90.    gleSetJoinStyle (TUBE_JN_ANGLE | TUBE_CONTOUR_CLOSED | TUBE_JN_CAP);
  91. }
  92.  
  93. float up_vector[3] = {0.0, -1.0, 0.0};
  94.  
  95. float lastx=0;
  96. float lasty=0;
  97. float newx=0;
  98. float newy=0;
  99.  
  100. /* ------------------------------------------------------- */
  101. /* draw the extrusion */
  102.  
  103. void DrawCyl (void) {
  104.    double moved_contour [NCONTOUR][2];
  105.    int style, save_style;
  106.    int i;
  107.  
  108.    for (i=0; i<cidx; i++) {
  109.       moved_contour[i][0] = contour_points [i][0];
  110.       moved_contour[i][1] = contour_points [i][1] + 0.05 * (newy-200);
  111.    }
  112.  
  113.    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  114.  
  115.    /* set up some matrices so that the object spins with the mouse */
  116.    glPushMatrix ();
  117.    glTranslatef (0.0, 8.0, -180.0);
  118.    glRotatef (0.5*lastx, 0.0, 1.0, 0.0);
  119.  
  120.    gleExtrusion (cidx, moved_contour, contour_points, up_vector, 
  121.                  idx, points, colors);
  122.  
  123.    glPopMatrix ();
  124.  
  125.  
  126.    /* draw a seond copy, this time with the raw style, to compare
  127.     * things against */
  128.    glPushMatrix ();
  129.    glTranslatef (0.0, -8.0, -180.0);
  130.    glRotatef (0.5*lastx, 0.0, 1.0, 0.0);
  131.  
  132.    save_style = gleGetJoinStyle ();
  133.    style = save_style;
  134.    style &= ~TUBE_JN_MASK;
  135.    style |= TUBE_JN_RAW;
  136.    gleSetJoinStyle (style);
  137.  
  138.    gleExtrusion (cidx, moved_contour, contour_points, up_vector, 
  139.                  idx, points, colors);
  140.  
  141.    gleSetJoinStyle (save_style);
  142.    glPopMatrix ();
  143.  
  144.    glutSwapBuffers ();
  145. }
  146.  
  147. /* ------------------------------------------------------- */
  148. /* get notified of mouse motions */
  149. void MouseMotion (int x, int y)
  150. {
  151.    lastx = newx;
  152.    lasty = newy;
  153.    newx = x;
  154.    newy = y;
  155.    glutPostRedisplay ();
  156. }
  157.  
  158. /* ------------------------------------------------------- */
  159. void JoinStyle (int msg) 
  160. {
  161.    int style;
  162.    /* get the current joint style */
  163.    style = gleGetJoinStyle ();
  164.  
  165.    /* there are four different join styles, 
  166.     * and two different normal vector styles */
  167.    switch (msg) {
  168.       case 0:
  169.          style &= ~TUBE_JN_MASK;
  170.          style |= TUBE_JN_RAW;
  171.          break;
  172.       case 1:
  173.          style &= ~TUBE_JN_MASK;
  174.          style |= TUBE_JN_ANGLE;
  175.          break;
  176.       case 2:
  177.          style &= ~TUBE_JN_MASK;
  178.          style |= TUBE_JN_CUT;
  179.          break;
  180.       case 3:
  181.          style &= ~TUBE_JN_MASK;
  182.          style |= TUBE_JN_ROUND;
  183.          break;
  184.  
  185.       case 20:
  186.          style &= ~TUBE_NORM_MASK;
  187.          style |= TUBE_NORM_FACET;
  188.          break;
  189.       case 21:
  190.          style &= ~TUBE_NORM_MASK;
  191.          style |= TUBE_NORM_EDGE;
  192.          break;
  193.  
  194.       case 99:
  195.          exit (0);
  196.  
  197.       default:
  198.          break;
  199.    }
  200.    gleSetJoinStyle (style);
  201.    glutPostRedisplay ();
  202. }
  203.  
  204. /* ------------------------------------------------------- */
  205. /* set up a light */
  206. GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
  207. GLfloat lightOneColor[] = {0.99, 0.99, 0.99, 1.0}; 
  208.  
  209. GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
  210. GLfloat lightTwoColor[] = {0.99, 0.99, 0.99, 1.0}; 
  211.  
  212. /* ------------------------------------------------------- */
  213. main (int argc, char * argv[]) {
  214.  
  215.    /* initialize glut */
  216.    glutInit (&argc, argv);
  217.    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  218.    glutCreateWindow ("cylinder");
  219.    glutDisplayFunc (DrawCyl);
  220.    glutMotionFunc (MouseMotion);
  221.  
  222.    /* create popup menu */
  223.    glutCreateMenu (JoinStyle);
  224.    glutAddMenuEntry ("Raw Join Style", 0);
  225.    glutAddMenuEntry ("Angle Join Style", 1);
  226.    glutAddMenuEntry ("Cut Join Style", 2);
  227.    glutAddMenuEntry ("Round Join Style", 3);
  228.    glutAddMenuEntry ("------------------", 9999);
  229.    glutAddMenuEntry ("Facet Normal Vectors", 20);
  230.    glutAddMenuEntry ("Edge Normal Vectors", 21);
  231.    glutAddMenuEntry ("------------------", 9999);
  232.    glutAddMenuEntry ("Exit", 99);
  233.    glutAttachMenu (GLUT_MIDDLE_BUTTON);
  234.  
  235.    /* initialize GL */
  236.    glClearDepth (1.0);
  237.    glEnable (GL_DEPTH_TEST);
  238.    glClearColor (0.0, 0.0, 0.0, 0.0);
  239.    glShadeModel (GL_SMOOTH);
  240.  
  241.    glMatrixMode (GL_PROJECTION);
  242.    /* roughly, measured in centimeters */
  243.    glFrustum (-9.0, 9.0, -9.0, 9.0, 50.0, 1150.0);
  244.    glMatrixMode(GL_MODELVIEW);
  245.  
  246.    /* initialize lighting */
  247.    glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
  248.    glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
  249.    glEnable (GL_LIGHT0);
  250.    glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition);
  251.    glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor);
  252.    glEnable (GL_LIGHT1);
  253.    glEnable (GL_LIGHTING);
  254.    glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
  255.    glEnable (GL_COLOR_MATERIAL);
  256.  
  257.    InitCyl ();
  258.  
  259.    glutMainLoop ();
  260. }
  261. /* ------------------ end of file ----------------------------- */
  262.